package edu.ynmd.BMI.service.impl;

import edu.ynmd.BMI.dao.AreaDao;
import edu.ynmd.BMI.dao.CityDao;
import edu.ynmd.BMI.dao.ProvinceDao;
import edu.ynmd.BMI.dao.StuinfoEntityDao;
import edu.ynmd.BMI.model.Area;
import edu.ynmd.BMI.model.City;
import edu.ynmd.BMI.model.Province;
import edu.ynmd.BMI.model.StuinfoEntity;
import edu.ynmd.BMI.service.ManageService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ManageServiceImpl implements ManageService{

    @Autowired
    private StuinfoEntityDao stuinfoEntityDao;

    @Autowired
    public ProvinceDao provinceDao;

    @Autowired
    public CityDao cityDao;
    @Autowired
    public AreaDao areaDao;


    @Override
    public boolean addStuinfoEntity(StuinfoEntity stuinfoEntity) {
        try {
            stuinfoEntityDao.save(stuinfoEntity);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public boolean deleteStuinfoEntity(String stuinfoEntityid) {
        try {
            stuinfoEntityDao.deleteById(stuinfoEntityid);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }



    }

    @Override
    public boolean updateStuinfoEntity(StuinfoEntity stuinfoEntity) {
        try {
            stuinfoEntityDao.save(stuinfoEntity);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }



    }

    @Override
    public StuinfoEntity getSingleStuinfoEntity(String stuinfoEntityid) {
        try {
            Optional<StuinfoEntity> optionstuinfo=stuinfoEntityDao.findById(stuinfoEntityid);
            return optionstuinfo.get();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<StuinfoEntity> getStuinfoEntityList() {
        return stuinfoEntityDao.findAll();
    }



    @Override
    public String getProvinceNameByCode(String sfbm) {
        List<Province> pl=provinceDao.getProvinceByCode(sfbm);
        if(pl.size()>0){
            return pl.get(0).getName();
        }
        else {
            return "";
        }


    }

    @Override
    public String getCityNameByCode(String citycode) {
        List<City> cl =cityDao.getCitiesByCode(citycode);
        if(cl.size()>0){
            return cl.get(0).getName();
        }


        return "";
    }

    @Override
    public String getAreaNameByCode(String areaCode) {
        List<Area> al =areaDao.getAreasByCode(areaCode);
        if(al.size()>0){
            return al.get(0).getName();
        }


        return "";
    }


    @Override
    public String getSexNameByStr(String sexstr) {
        int si =Integer.parseInt(sexstr);
        if (si%2==0){
            return "女";
        }
        else{
            return "男";
        }

    }
}
